// const add = require("../../../router/home/add")

// const { default: axios } = require("axios")
// const { count } = require("console")

// const { count } = require("console")

// const { default: axios } = require("axios")

// const { default: axios } = require("axios")

//切换收货地址
function changeAddress(){``
    let addrItem = document.getElementsByClassName('addrItem')
    let defaultAddress = document.getElementsByClassName('defaultAddress')[0]
    let nextRadioLabel = document.getElementsByClassName('nextRadioLabel')
    let selectDesc = document.querySelectorAll('.selectDesc')
    for(let i = 0; i<addrItem.length; i++){
        addrItem[i].addEventListener('click', function(){
            for(let j =0; j<addrItem.length; j++){
                addrItem[j].classList.remove('defaultAddress')
                selectDesc[j].classList.add('selectNone')
                nextRadioLabel[j].classList.remove('defaultDet')
            }
            addrItem[i].classList.add('defaultAddress')
            selectDesc[i].classList.remove('selectNone')
            nextRadioLabel[i].classList.add('defaultDet')
            GoodsInfo()
        })
    }
}
changeAddress()
//修改默认地址
function changeDefault(){
    let addrItem = document.getElementsByClassName('addrItem')
    let nextRadioLabel = document.getElementsByClassName('nextRadioLabel')
    for(let i = 0 ; i < addrItem.length; i++){
        if( addrItem[i] .classList.contains('defaultAddress') == true){
            if( document.getElementsByClassName('setDefault')[i] ){
                addrItem[i].removeChild(document.getElementsByClassName('setDefault')[i])
                nextRadioLabel[i].appendChild(document.getElementsByClassName('defaultDet')[i])
            }
            
        }
        return
    }
}
console.log(userId);
//遍历收货地址
function takeAddress(){
    let addressList = document.getElementsByClassName('addressList')[0]
    axios({
        url:'/home/creatOrder/address/queryAddress',
        method:'GET',
        params:{
            userId:userId
        }
    })
    .then(data => {
        console.log(data);
        console.log(data.data.data.data);
        addressList.innerHTML = ''
        for(let i in data.data.data.data){
            addressList.innerHTML += `
            <div class="addrItem" addressId='${data.data.data.data[i].addressId}'>
                <div class="selectDesc selectNone">
                    <i class="iconfont weizhi" style="font-size: 24px;color: #F96C0C;line-height: 26px;"></i>
                    <span>寄送至</span>
                </div>
                <label for="">
                    <span class="nextRadio"><input type="radio"></span>
                    <span class="nextRadioLabel defaultDet">
                        <span class="provinceName">${data.data.data.data[i].address}</span>
                        <span class="cityName"></span>
                        <span class="areaName"></span>
                        <span class="townName"></span>
                        <span class="adressDetail">华兰大道河南科技学院</span>
                        （<span class="hostName">${data.data.data.data[i].addressee}</span> 收）
                        <span class="mobile">${data.data.data.data[i].addresseeTele}</span>
                    </span>
                </label>
            </div>
            `
        }
        document.getElementsByClassName('addrItem')[0].classList.add('defaultAddress')
        document.getElementsByClassName('addrItem')[0].classList.remove('selectNone')
        changeAddress()
        GoodsInfo()

    })
    .catch(err => {
        console.log(err);
    })
}
takeAddress()


changeDefault()
// 改变购买数量
function changeCount(){
    let downBtn = document.getElementsByClassName('downBtn')[0]
    let upBtn = document.getElementsByClassName('upBtn')[0]
    let countInput = document.getElementsByClassName('countInput')[0]
    let i = countInput.value
    console.log(111);
    downBtn.onclick = function(){
        if(i<=1){
            downBtn.classList.add('accountCommon')
            
        }else{
            i--
            countInput.value = i
            if( i == 1){
                downBtn.classList.add('accountCommon') 
            }
        }
    }
    upBtn.onclick = function(){
        i++
        countInput.value = i
        downBtn.classList.remove('accountCommon')
    }
}
changeCount()

var strHref = document.location.toString();//js获取url地址
console.log(strHref);//打印出URL
var intPos = strHref.indexOf("?")+1;
// var strRight = strHref.substr(intPos + 1); //获取到右边的参数部分
strHref = strHref.substring(intPos)
var arr = strHref.split('&')
var obj = {}
for(let i =0; i<arr.length; i++){
    let str1 = arr[i].split('=')[0]
    let str2 = arr[i].split('=')[1]
    obj[str1] = str2
}
console.log(obj);
// 再次调用商品详情接口渲染商品信息
function GoodsInfo(){
    axios({
        url:'/home/productDetail/inGoodsPage',
        method:'GET',
        params:{
            goodsId:obj.goodsId
        }
    })
    .then(data => {
        console.log(data);
        let priceValue = document.getElementsByClassName('countInput')[0].value
        console.log(priceValue);
        console.log(document.querySelector('.sellerInfo a'));
        document.querySelector('.sellerInfo a').innerHTML = data.data.data.data.shopName
        document.querySelector('.infoCellData a').innerHTML = data.data.data.data.goodsName
        document.getElementsByClassName('infoPrice')[0].innerHTML = data.data.data.data.goodsPrice
        document.querySelector('.finallyPrice span').innerHTML = data.data.data.data.goodsPrice*priceValue
        document.getElementsByClassName('realPrice')[0].innerHTML = data.data.data.data.goodsPrice*priceValue
        document.getElementsByClassName('allPriceSpan')[0].innerHTML = data.data.data.data.goodsPrice*priceValue

        document.querySelector('.infoCell a img').src = data.data.data.data.cover
        // 收货信息
        let defaultAddress = document.getElementsByClassName('defaultAddress')[0]
        // 省
        let provinceName = defaultAddress.getElementsByClassName('provinceName')[0]
        // 市
        let cityName = defaultAddress.getElementsByClassName('cityName')[0]
        let areaName = defaultAddress.getElementsByClassName('areaName')[0]
        let townName = defaultAddress.getElementsByClassName('townName')[0]
        let adressDetail = defaultAddress.getElementsByClassName('adressDetail')[0]
        let hostName = defaultAddress.getElementsByClassName('hostName')[0]
        let mobile = defaultAddress.getElementsByClassName('mobile')[0]
        document.querySelector('.confirmAddr span').innerHTML = provinceName.innerHTML+cityName.innerHTML+areaName.innerHTML+townName.innerHTML+adressDetail.innerHTML
        document.querySelector('.confirmBuyer span').innerHTML = hostName.innerHTML+' '+mobile.innerHTML
    })
    .catch(err => {
        console.log(err);
    })
}
GoodsInfo()

//点击立即购买生成订单
let submitWrap = document.getElementsByClassName('submitWrap')[0]
submitWrap.addEventListener('click', function(){
    let defaultAddress = document.getElementsByClassName('defaultAddress')[0]
    axios({
        url:'/home/creatOrder/order/addOrder',
        method:'GET',
        params:{
            addressId:defaultAddress.getAttribute('addressId'),
            count:document.getElementsByClassName('countInput')[0].value,
            goodsId:obj.goodsId,
            shopId:obj.shopId,
            userId:userId
        }
    })
    .then(data=> {
        console.log(data);
        if(data.data.data.code === 20000){
            alert('生成订单成功')//跳转至支付页面。在支付地址上把订单id加上，加个密，如何支付
            // 去支付
            axios({
                url:'/home/creatOrder/payFor',
                method:'GET',
                params:{
                    orderId:data.data.data.data[0].orderId
                }
            })
            .then(data => {
                console.log(data);
                // 判断一下，支付成功给弹窗
                // explain({
                //     code:1,
                //     msg:'支付成功',
                //     time:2000
                // })
            })
            .catch(err => {
                console.log(err);
            })
        }else{
            explain({
                code:1,
                msg:'支付失败',
                time:2000
            })
        }
        
    })
    .catch(err=> {
        console.log(err);
    })
    // window.location.href = `/home/orderNo?shopId=${obj.shopId}&goodsId=${obj.goodsId}&color=${obj.color}&size=${obj.size}&count=${obj.count}`
})

